﻿//********************************************************************************//
//创建日期：<创建日期，2016-03-15>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：发票领用
//********************************************************************************//
layui.use(['form', 'jquery', 'element', 'laydate'], function () {
    var form = layui.form()
        , $ = layui.jquery
        , layer = layui.layer
        , element = layui.element()
    , laydate = layui.laydate;

    /*page load start*/
    initcontrol();
    initinfo();
    load();
    bindoperate(true);
    load2();
    bindoperate2();
    
    /*page load end*/

    /*initial start*/
    function initcontrol() {        
        //加载站点
    
        $("#eZhanDian option").remove();
        var list = getZDList("","_");
        for (var i = 0; i < list.length; i++) {
            $("#eZhanDian").append("<option value='" + list[i]["Value"] + "'>" + list[i]["Text"] + "</option>");
        } 
        $("#eZhanDian").get(0).selectedIndex = 0;
        //加载管理块
        $("#eGuanLiK option").remove();
        list = getGLKList("","_");
        for (var i = 0; i < list.length; i++) {
            $("#eGuanLiK").append("<option value='" + list[i]["Value"] + "'>" + list[i]["Text"] + "</option>");
        }
        $("#eGuanLiK").get(0).selectedIndex = 0;
        form.render("select");
    }
    function initinfo() {
        $("#eCount").val("");
        $("#eStartNo").val("");
        $("#eEndNo").val("");
        $("#eTime").val("");
        $("#eUser").val("");
        $("#eStartNo").attr("disabled", "disabled");
        $("#eEndNo").attr("disabled", "disabled");
        $("#ObjId").attr("vid", "0");
        $("#ObjCount").attr("vid", "0");
    }
    /*initial end*/

    /*list show start*/
    function createItemStr(obj) {
        var str = $("#ItemStr tr").prop("outerHTML");
        var itemstr, id, count, start, end, time, user, use, remain;
        id = obj["ID"];
        count = obj["I_SHULIANG"];
        start = obj["S_QISHIHM"];
        end = obj["S_JIESHUHM"];
        time = obj["D_RUKURQ"]["DateString"];
        user = obj["S_RUKUR"];
        use = obj["I_LINGYONGSL"];
        remain = obj["I_SHENGYUSL"];
        itemstr = str.replace(new RegExp("objid", "gm"), id).replace(new RegExp("objCount", "gm"), count)
                        .replace(new RegExp("objStart", "gm"), start).replace(new RegExp("objEnd", "gm"), end)
                        .replace(new RegExp("objTime", "gm"), time).replace(new RegExp("objUser", "gm"), user)
                        .replace(new RegExp("objUse", "gm"), use).replace(new RegExp("objRemain", "gm"), remain)
        ;
        return itemstr;
    }
    function load() {
        var index = layer.load(1);
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/YSZW/FaPiao.ashx",
            data: {
                "op": "KCList"
            },
            dataType: "json",
            success: function (rs) {
                var strs = "";
                for (var i in rs) {
                    strs = strs + createItemStr(rs[i]);
                }
                $("#ContentList").find(".datarow").remove();
                $("#ContentList").append(strs);
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        layer.close(index);
    }
    function bindoperate(load) {
        $("#ContentList .datarow").unbind("click").click(function () {
            //加载库存明细
            var vid = $(this).attr("vid");
            load3(vid);
            bindoperate3();
        });
        //是否默认加载列表1中第一项对应的明细
        if(load)
        {
            var itemT = $("#ContentList .datarow");
            if (itemT.length > 0)
            {
                itemT.eq(0).click();
            }
        }
    }

    function createItemStr2(obj) {
        var str = $("#ItemStr2 tr").prop("outerHTML");
        var itemstr, id, zd, glk, receiver, recieveCount, start, end, used, remainStart,remainCount,receiveTime,operator;
        id = obj["ID"];
        zd = obj["S_ZHANDIANMC"];
        glk = obj["S_GUANLIKMC"];
        receiver = obj["S_LINGYONGR"];
        recieveCount = obj["I_LINGYONGSL"];
        start = obj["S_LINGYONGQSHM"];
        end = obj["S_LINGYONGJSHM"];
        used = obj["I_SHIYONGSL"];
        remainStart = obj["S_SHENGYUQSHM"];
        remainCount = obj["I_SHENGYUSL"];
        receiveTime = obj["D_LINGYONGRQ"]["DateString"];
        operator = obj["S_CAOZUORMC"];
        itemstr = str.replace(new RegExp("objid", "gm"), id)
                        .replace(new RegExp("objZhanDian", "gm"), zd).replace(new RegExp("objGuanLiK", "gm"), glk)
                        .replace(new RegExp("objReceiver", "gm"), receiver).replace(new RegExp("objReceiveCount", "gm"), recieveCount)
                        .replace(new RegExp("objStartNo", "gm"), start).replace(new RegExp("objEndNo", "gm"), end)
                        .replace(new RegExp("objUsedCount", "gm"), used).replace(new RegExp("objRemainStartNo", "gm"), remainStart)
                        .replace(new RegExp("objRemainCount", "gm"), remainCount).replace(new RegExp("objReceiveTime", "gm"), receiveTime)
                        .replace(new RegExp("objOperator", "gm"), operator)
        ;
        return itemstr;
    }
    function load2() {
        var index = layer.load(1);
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/YSZW/FaPiao.ashx",
            data: {
                "op": "LYList"
            },
            dataType: "json",
            success: function (rs) {
                var strs = "";
                for (var i in rs) {
                    strs = strs + createItemStr2(rs[i]);
                }
                $("#ContentList2").find(".datarow").remove();
                $("#ContentList2").append(strs);
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        layer.close(index);
    }
    function bindoperate2() {
        $(".aQXLY").unbind("click").click(function () {
            var vid = $(this).attr("vid");
            layer.confirm('确认要取消吗?', function () {                
                $.ajax({
                    type: "post",
                    cache: false,
                    async: false,
                    url: "/ashx/YSZW/FaPiao.ashx",
                    data: {
                        "op": "LYQX",
                        "id": vid
                    },
                    dataType: "json",
                    success: function (rs) {
                        layer.msg(rs.Msg);
                        if (rs.State) {
                            load2();
                            bindoperate2();
                            load();
                            bindoperate(true);

                        }
                    },
                    error: function (error) {
                        layer.msg(error.responseText);
                    }
                });
            })           
        });
    }
    function createItemStr3(obj) {
        var str = $("#ItemStr3 tr").prop("outerHTML");
        var itemstr, id, count, start, end, desc,time,operator;
        id = obj["ID"];
        count = obj["I_SHULIANG"];
        start = obj["S_QISHIHM"];
        end = obj["S_JIESHUHM"];
        desc = obj["S_MIAOSHU"];
        time = obj["D_CAOZUOSJ"]["DateString"];
        operator = obj["S_CAOZUOR"];
        itemstr = str.replace(new RegExp("objid", "gm"), id)
                        .replace(new RegExp("objCount", "gm"), count)
                        .replace(new RegExp("objStart", "gm"), start)
                        .replace(new RegExp("objEnd", "gm"), end)
                        .replace(new RegExp("objDesc", "gm"), desc)
                        .replace(new RegExp("objTime", "gm"), time)
                        .replace(new RegExp("objOperator", "gm"), operator)
        ;
        return itemstr;
    }
    function load3(kcid) {
        var index = layer.load(1);
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/YSZW/FaPiao.ashx",
            data: {
                "op": "KCMXList",
                "id":kcid
            },
            dataType: "json",
            success: function (rs) {
                var strs = "";
                for (var i in rs) {
                    strs = strs + createItemStr3(rs[i]);
                }
                $("#ContentList3").find(".datarow").remove();
                $("#ContentList3").append(strs);
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        layer.close(index);
    }
    function bindoperate3() {
        $("#ContentList3 .datarow").unbind("click").click(function () {
            //加载信息到领用信息中
            var vid = $(this).attr("vid");
            var tds = $("#ContentList3 tr[vid='" + vid + "']").find("td");
            //第2列为当前开始号码
            var start = tds[1].innerText;
            //第1列为剩余数量
            var count = tds[0].innerText;
            $("#eStartNo").val(start);
            //第一次加载默认入库人,时间
            if ($("#eUser").val() == "") {
                var date = new Date()
                $("#eTime").val(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
                var logininfo = getLoginInfo();
                $("#eUser").val(logininfo.UserName);
            }
            //聚焦到领用数量
            $("#eCount").focus();
            //记录当前vid,剩余数量
            $("#ObjId").attr("vid", vid);
            $("#ObjCount").attr("vid", count);
        });
    }
    /*list show end*/

    /*operate start*/
    function clearinfo() {
        $("#eCount").val("");
        $("#eStartNo").val("");
        $("#eEndNo").val("");
        $("#ObjId").attr("vid", "0");
        $("#ObjCount").attr("vid", "0");
    }    
    $("#btnRefresh1").click(function () {
        load();
        bindoperate(true);
    });
    $("#btnSave").click(function () {
        if (verify() == false) {
            return;
        }
        var id = $("#QuestionId").val();
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/YSZW/FaPiao.ashx",
            data: {
                "op": "LY",
                "eFaPiaoKCMXID":$("#ObjId").attr("vid"),
                "eZhanDian": $("#eZhanDian").val(),
                "eGuanLiK": $("#eGuanLiK").val(),
                "eCount": $("#eCount").val(),
                "eStartNo": $("#eStartNo").val(),
                "eEndNo": $("#eEndNo").val(),
                "eTime": $("#eTime").val(),
                "eUser": $("#eUser").val()
            },
            dataType: "json",
            success: function (rs) {
                layer.msg(rs.Msg);
                if (rs.State) {
                    clearinfo();
                    load();
                    bindoperate(true);
                    load2();
                    bindoperate2();
                }
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
    });
    /*operate end*/

    /*logic start*/
    function verify() {
        //判断是否选择了发票段
        if ($("#ObjId").attr("vid") == "0") {
            layer.alert("请在库存明细中选择要领用的发票。");
            return false;
        }
        if (!verEmpty($("#eCount").val())) {
            layer.alert("请输入数量!", function (index) {
                layer.close(index);
                $("#eCount").focus();
            });
            return false;
        }
        //判断数量       
        if ($("#eCount").val()=="0")
        {
            layer.alert("请填写正确的领用数量。");
            return false;
        }
        if (parseInt($("#eCount").val()) > parseInt($("#ObjCount").attr("vid"))) {
            layer.alert("领用数量不能超过剩余数量。", function (index) {
                layer.close(index);
                $("#eCount").select();
            });
            return false;
        }
        if (!verEmpty($("#eTime").val())) {
            layer.alert("请填写入库时间!");
            return false;
        }
        if (!verEmpty($("#eUser").val())) {
            layer.alert("请填写入库人!");
            return false;
        }
        return true;
    }
    function recountEndNo() {
        var start = $("#eStartNo").val();
        if (start == "") {
            $("#eEndNo").val("");
            return;
        }
        var count = $("#eCount").val();
        if (count == "") {
            $("#eEndNo").val("");
            return;
        }
        var end = parseFloat(start) + parseFloat(count)-1;
        $("#eEndNo").val(end);
    }
    $("#eStartNo").blur(function () {
        recountEndNo();
    });
    $("#eCount").blur(function () {

        recountEndNo();
    });
    /*logic end*/
});
